/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

package com.zog
{
    import com.pblabs.engine.components.TickedComponent;
	import com.pblabs.engine.entity.EntityComponent;
    import com.pblabs.engine.PBE;
    import com.pblabs.engine.debug.Logger;
    import com.pblabs.engine.entity.IEntityComponent;
    import com.pblabs.engine.entity.IEntity;
    import flash.geom.Point;
	
	public class EntitySpawner extends TickedComponent
	{
        /**
         * Spawn interval in seconds.
         */
        public var interval:Number = 10.0;
        
        public var templateName:String = "";
        
        public var updateParamsEachSpawn:Boolean = false;

        public function get paramString():String
        {
            return _paramString;
        }
        
        public function set paramString(str:String):void
        {
            _paramString = str;
            evaluateParamString();
        }

        
        private var params:Object = null;
        private var currentInterval:Number = 0.0;
        private var _paramString:String = "";
        
		protected override function onAdd():void
		{
			super.onAdd();
		}
		
		protected override function onRemove():void
		{
			super.onRemove();
		}
        
        private function evaluateParamString():void
        {
			//Logger.print(this, "evaluateString:" + argsString);
			
            //EVENT ARGS:
            //[NAME]|[TYPE]|[VALUE],[NAME]|[TYPE]|[VALUE],...
            //@Spatial.position|flash.geom::Point|10.3/23.5,health|Number|25,rotation|Ref|#Player.Spatial.rotation
            //@Spatial.position|Point|#Player.Spatial.position
            var args:Array = _paramString.split(",");
            
            params = new Object();
            
            for each(var arg:String in args)
            {
                //e.g. position|flash.geom::Point|10.3/23.5
                var paramBits:Array = arg.split("|");
                
                //params = new Object();
                
                var component:IEntityComponent = checkForComponentReference(paramBits[2]);
                var curLookup:String = "";
                var names:Array = null;
                
                switch(paramBits[1])
                {
                    case "Number":
                    {
                        if (component != null)
                        {
                            curLookup = paramBits[2].slice(1);
                            names = curLookup.split(".");

                            var n:Number;
                            //Player.Spatial.position
                            switch(names.length)
                            {
                                case 3:
                                {
                                    n = component[names[2]];
                                }
                                break;
                                
                                case 4:
                                {
                                    n = component[names[2]][names[3]];
                                }
                                break;
                            }
                            params[paramBits[0]] = n;
                        }
                        else
                        {
                            params[paramBits[0]] = parseFloat(paramBits[2]);
                        }
                    }
                    break;
                    
                    case "Point":
                    {
                        if (component != null)
                        {
                            curLookup = paramBits[2].slice(1);
                            names = curLookup.split(".");

                            var p:Point = component[names[2]];
                            params[paramBits[0]] = (component[names[2]] as Point);
                        }
                        else
                        {
                            var values:Array = paramBits[2].split("/");
                            //var f:Number = parseFloat(values[0]);
                            //var f2:Number = parseFloat("13.4");
                            //Logger.print(this, f2.toString());
                            params[paramBits[0]] = new Point( parseFloat(values[0]), parseFloat(values[1]) );
                        }
                    }
                    break;
                }
            }
            
            //_argumentString = argsString;
        }
        
        private function checkForComponentReference(path:String):*
        {
            var c:IEntityComponent = null;
            
            var startChar:String = path.charAt(0);
            if(startChar == "#")
            {
                var curLookup:String = path.slice(1);
                var names:Array = curLookup.split(".");
                var ent:IEntity = PBE.nameManager.lookup(names[0]);
                c = ent.lookupComponentByName(names[1]);
            }
            
            return c;
        }
        
		
        //@param deltaTime The amount of time (in seconds) specified for a tick.
        public override function onTick(deltaTime:Number):void
        {
            currentInterval += deltaTime;
            
            if(currentInterval >= interval)
            {
                currentInterval = 0;
                
                if(templateName != "" )
                {
                    if(updateParamsEachSpawn == true)
                    {
                        evaluateParamString();
                    }
                    PBE.makeEntity(templateName, params);
                }
                else
                {
                    Logger.warn(this, "onTick", "template name is empty!");
                }
            }
        }
   	}
}